package org.docmanagement.services.impl;

import java.util.List;

import org.docmanagement.data.Configuration;
import org.docmanagement.services.ConfigurationException;
import org.docmanagement.services.ConfigurationService;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("configurationService")
@Transactional
public class ConfigurationServiceImpl extends HibernateDaoSupport implements ConfigurationService {

	@Autowired
	public ConfigurationServiceImpl(SessionFactory sessionFactory) {
		super();
		setSessionFactory(sessionFactory);
	}

	public Configuration getConfiguration() throws ConfigurationException {
		List<Configuration> l = getHibernateTemplate().loadAll(Configuration.class);
		if (l.size() == 0) {
			return new Configuration();
		}
		return (Configuration) l.get(l.size() - 1);
	}

	public void setConfiguration(Configuration configuration) throws ConfigurationException {
		getHibernateTemplate().saveOrUpdate(configuration);
	}
}
